<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <script type="text/javascript">
    jsonp({
      url: "http://localhost:3001/better",
      success: function (data) {
        console.log(data);
      },
    });

    function jsonp(options) {
      var fnName = "fnName" + Math.random().toString().replace(".", "");
      var params = "";
      for (var attr in options.data) {
        params += "&" + attr + "=" + options.data[attr];
      }
      // 将方法挂载到对象上 
      window[fnName] = options.success; // window.fnName=options.success 
      var script = document.createElement("script");
      script.src = options.url + "?callback=" + fnName + params;
      document.body.appendChild(script);
      script.onload = function () {
        document.body.removeChild(script);
      };
    }
  </script>
</body>

</html>